﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Wordpress.Phone.ViewModels;
using Wordpress.Phone.Models;
using System.Windows.Media.Imaging;

namespace Wordpress.Phone.Views
{
    public partial class DetailPage : PhoneApplicationPage
    {
        protected DetailViewModel viewModel;
        
        public DetailPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (this.viewModel == null) {
                this.viewModel = new DetailViewModel();
                DataContext = this.viewModel;
            }

            string selectedIndex = "";
            if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
            {
                int index = int.Parse(selectedIndex);
                this.viewModel.Load(index);
            }
        }

        private void WatchElement_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Views/MoviePage.xaml?selectedItem="+this.viewModel.CurrentIndex, UriKind.Relative));
        }

        private void Thumbnail_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
            Image img = sender as Image;
            img.Source = new BitmapImage(new Uri("/Wordpress.Phone;component/Assets/Images/Thumbnail.png", UriKind.Relative));
        }
    }
}